package com.SFA.OrderManagement;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.FrameWork.Common.MyApplication;
import com.FrameWork.DB.DataProvider;
import com.FrameWork.Entity.CustomerInfoEntity;
import com.FrameWork.Entity.DataEntity;
import com.FrameWork.Entity.GlobalInfo;
import com.FrameWork.Entity.InputType;
import com.FrameWork.Entity.PhotoEntity;
import com.FrameWork.Entity.ProductEntity;
import com.FrameWork.Utils.Utils;
import com.SFA.R;
import com.umeng.analytics.MobclickAgent;

/**
 * 
 * @author Steven Jiang
 * @since 2012年5月15日
 * 
 */
public class AuditCPRManager {
	private Context owner;
	@SuppressWarnings("unused")
	private CustomerInfoEntity entity = null;

	public AuditCPRManager(Context owner, CustomerInfoEntity entity) {
		this.entity = entity;
		this.owner = owner;

	}
	
	public List<DataEntity> getDataEntityList(boolean CONTENDER, int DataCategory) {
		SQLiteCursor cursor = null;
		List<DataEntity> list= new ArrayList<DataEntity>();
		String sql= null;
		try {
			if(DataCategory == 2)
				sql= String.format(owner.getString(R.string.sql_GetBrands), CONTENDER ? "True":"False");
			
			cursor = DataProvider.Instance(owner).Query(sql);
			while(cursor.moveToNext())
			{
				DataEntity entity = new DataEntity();
				entity.CODE = cursor.getString(cursor.getColumnIndex("CODE"));
				entity.ID=cursor.getString(cursor.getColumnIndex("ID"));
				entity.NAME=cursor.getString(cursor.getColumnIndex("NAME"));
				entity.CONTENDER = CONTENDER;
				list.add(entity);
			}
		} catch (Exception ex) {
			MobclickAgent.reportError(owner, ex.toString());
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return list;
	}
	
	public List<ProductEntity> getDataEntityByForm(String FormCode) {
		SQLiteCursor cursor = null;
		List<ProductEntity> list= new ArrayList<ProductEntity>();
		String sql= null;
		try {
			sql= String.format(owner.getString(R.string.sql_GetCprProductsByFormCode),entity.CUSTOMER_PRODUCT_LIST_ID, FormCode);
			
			cursor =DataProvider.Instance(owner).Query(sql);
			while(cursor.moveToNext())
			{
				ProductEntity entity = new ProductEntity();
				entity.CODE = cursor.getString(cursor.getColumnIndex("CODE"));
				entity.UOM_CODE = cursor.getString(cursor.getColumnIndex("UOM_CODE"));
				entity.ID=cursor.getString(cursor.getColumnIndex("ID"));
				entity.DESCRIPTION= cursor.getString(cursor.getColumnIndex("DESCRIPTION"));
				entity.SHORTDESCRIPTION=cursor.getString(cursor.getColumnIndex("SHORT_DESCRIPTION"));
				entity.CASE_PRICE=cursor.getFloat(cursor.getColumnIndex("CASE_PRICE"));
				entity.DENOMINATOR = cursor.getInt(cursor.getColumnIndex("DENOMINATOR"));
				entity.PIECE_PRICE = cursor.getFloat(cursor.getColumnIndex("PIECE_PRICE"));
				entity.CONTENDER = false;
				list.add(entity);
			}
		} catch (Exception ex) {
			MobclickAgent.reportError(owner, ex.toString());
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return list;
	}
	
	public List<ProductEntity> QueryProduct(String keyword) {
		SQLiteCursor cursor = null;
		List<ProductEntity> list= new ArrayList<ProductEntity>();
		String sql= null;
		try {
			sql= String.format(owner.getString(R.string.sql_QueryProduct),entity.CUSTOMER_PRODUCT_LIST_ID);
			
			String format = "	AND (P.FORM_CODE like '%" +keyword + "%' OR p.CODE  LIKE '%" +keyword + "%'   OR p.SHORT_DESCRIPTION LIKE '%" +keyword + "%'  OR p.DESCRIPTION LIKE '%" +keyword + "%' )";
			sql += format + " limit " +  10 + " offset 0";
			cursor =DataProvider.Instance(owner).Query(sql);
			while(cursor.moveToNext())
			{
				ProductEntity entity = new ProductEntity();
				entity.CODE = cursor.getString(cursor.getColumnIndex("CODE"));
				entity.UOM_CODE = cursor.getString(cursor.getColumnIndex("UOM_CODE"));
				entity.ID=cursor.getString(cursor.getColumnIndex("ID"));
				entity.DESCRIPTION= cursor.getString(cursor.getColumnIndex("DESCRIPTION"));
				entity.SHORTDESCRIPTION=cursor.getString(cursor.getColumnIndex("SHORT_DESCRIPTION"));
				entity.CASE_PRICE=cursor.getFloat(cursor.getColumnIndex("CASE_PRICE"));
				entity.DENOMINATOR = cursor.getInt(cursor.getColumnIndex("DENOMINATOR"));
				entity.PIECE_PRICE = cursor.getFloat(cursor.getColumnIndex("PIECE_PRICE"));
				entity.CONTENDER = false;
				list.add(entity);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			MobclickAgent.reportError(owner, ex.toString());
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return list;
	}

	@SuppressWarnings({ "finally", "static-access" })
	private boolean insert2DB(String tableName,
			ArrayList<ContentValues> arrayValues) {
		boolean iserror = true;
		SQLiteDatabase db = MyApplication.getInstance().DataProvider
				.getWritableDatabase();
		db.beginTransaction();
		try {
			for (ContentValues c : arrayValues) {
				long rowId = db.insertWithOnConflict(tableName, null, c,db.CONFLICT_IGNORE);
				long rows=db.updateWithOnConflict(tableName, c, "ID=?", new String[]{ c.getAsString("ID")}, db.CONFLICT_IGNORE);
				if (rowId == -1&&rows==-1) {
					iserror = false;
					throw new Exception("出现插入冲突(Conflict)！返回值rowId为-1。");
				}
			}
			db.setTransactionSuccessful();
		} catch (Exception ex) {
			iserror = false;
			MobclickAgent.reportError(owner, "数据插入异常：" + ex.toString());
		} finally {
			db.endTransaction();
			return iserror;
		}
	}

	public List<DataEntity> getFormList(String BrandCode)
	{
		SQLiteCursor cursor = null;
		List<DataEntity> list= new ArrayList<DataEntity>();
		String sql= null;
		try {
			sql= String.format(owner.getString(R.string.sql_GetSeriesItems), BrandCode);
			cursor =DataProvider.Instance(owner).Query(sql);
			while(cursor.moveToNext())
			{
				DataEntity entity = new DataEntity();
				entity.CODE = cursor.getString(cursor.getColumnIndex("CODE"));
				entity.ID=cursor.getString(cursor.getColumnIndex("ID"));
				entity.NAME=cursor.getString(cursor.getColumnIndex("NAME"));
				list.add(entity);
			}
		} catch (Exception ex) {
			MobclickAgent.reportError(owner, ex.toString());
		} finally {
			if (cursor != null) {
				cursor.close();
				cursor = null;
			}
		}
		return list;
	}
	
}
